123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- const utils = require('../../api/utils')
- const processDataSet = require('../utils/processDataSet')
- Component({
- data: {
- isUpdating: false,
- scope: '',
- getAuthorize: '',
- styleV2: my.styleV2,
- },
- props: {
- size: 'default',
- type: '',
- plain: false,
- disabled: false,
- loading: false,
- 'form-type': '',
- 'open-type': '',
- 'app-parameter': '',
- 'hover-class': 'button-hover',
- 'hover-stop-propagation': false,
- 'hover-start-time': false,
- className: '',
- onTap: () => {},
- onGetUserInfo: '',
- onGetPhoneNumber: '',
- },
- onInit() {
- this.updateData()
- },
- deriveDataFromProps(nextProps) {
- const {
- size,
- type,
- plain,
- disabled,
- loading,
- formType,
- openType,
- hoverClass,
- hoverStopPropagation,
- hoverStartTime,
- appParameter,
- } = nextProps
- const opentype = this.getOpenType(openType)
- const scope = this.getScope(openType)
- const params = {
- size,
- type,
- plain,
- disabled,
- loading,
- formType,
- opentype,
- scope,
- hoverClass,
- hoverStopPropagation,
- hoverStartTime,
- appParameter,
- }
- Object.keys(params).forEach(
- (key) => params[key] === undefined && delete params[key],
- )
- this.setData(params)
- },
- methods: {
- updateData() {
- if (this.data.isUpdating) {
- return
- }
- this.setData({
- isUpdating: true,
- })
- for (const key in this.props) {
- if (this.props.hasOwnProperty(key)) {
- typeof this.props[key] === 'string'
- && (this.props[key] = this.props[key].replace(/(^\s*)|(\s*$)/g, ''))
- }
- }
- const {
- size,
- type,
- plain,
- disabled,
- loading,
- formType,
- hoverClass,
- hoverStopPropagation,
- hoverStartTime,
- appParameter,
- openType,
- } = this.props
- this.getSystem(() => {
- const opentype = this.getOpenType(openType)
- const scope = this.getScope(openType)
- this.setData({
- isUpdating: false,
- size,
- type,
- plain,
- disabled,
- loading,
- formType,
- opentype,
- hoverClass,
- hoverStopPropagation,
- hoverStartTime,
- appParameter,
- scope,
- })
- })
- },
- getSystem(cb) {
- const that = this
- my.getSystemInfo({
- success(res) {
- let app = ''
- if (res.app && res.app === 'amap') {
- app = 'amap'
- } else {
- app = 'alipay'
- }
- that.setData({
- app,
- })
- cb()
- },
- })
- },
- getOpenType(opentype) {
- const transformList = {
- getPhoneNumber: 'getAuthorize',
- getUserInfo: 'getAuthorize',
- }
- if (transformList[opentype]) {
- return transformList[opentype]
- }
- let allowList = ['share', 'launchApp', 'getAuthorize', 'openSetting']
- if (this.data.app === 'amap') {
- allowList = ['share', 'getAuthorize', 'openSetting']
- }
- if (opentype) {
- if (allowList.indexOf(opentype) !== -1) {
- return opentype
- } else {
- utils.warn(`小程序open-type值不支持${opentype}`, {
- apiName: `button/open-type/${opentype}`,
- errorType: 0,
- type: 'component',
- })
- }
- }
- return ''
- },
- getScope(opentype) {
- const scopeMap = {
- getPhoneNumber: 'phoneNumber',
- getUserInfo: 'userInfo',
- }
- return scopeMap[opentype] || ''
- },
- onError(err) {
- if (this.props.onError === 'function') {
- this.props.onError(err)
- }
- },
- getAuthorize() {
- const that = this
- const resObj = {}
- if (
- this.data.opentype === 'getAuthorize'
- && this.data.scope === 'phoneNumber'
- ) {
- my.getPhoneNumber({
- success: (res) => {
- if (typeof that.props.onGetPhoneNumber === 'function') {
- resObj.detail = res
- resObj.type = 'getphonenumber'
- that.props.onGetPhoneNumber(resObj)
- }
- },
- fail: (res) => {
- if (typeof that.props.onGetPhoneNumber === 'function') {
- resObj.detail = res
- resObj.type = 'getphonenumber'
- that.props.onGetPhoneNumber(resObj)
- }
- },
- })
- }
- if (
- this.data.opentype === 'getAuthorize'
- && this.data.scope === 'userInfo'
- ) {
- my.getOpenUserInfo({
- success: (res) => {
- if (typeof that.props.onGetUserInfo === 'function') {
- const _res = JSON.parse(res.response).response
- _res.gender && _res.gender === 'm'
- ? (_res.gender = 1)
- : (_res.gender = 2)
- _res.avatarUrl = _res.avatar
- delete _res.avatar
- resObj.detail = {}
- resObj.detail.userInfo = _res
- resObj.type = 'getuserinfo'
- that.props.onGetUserInfo(resObj)
- }
- },
- fail: (res) => {
- if (typeof that.props.onGetUserInfo === 'function') {
- resObj.detail = res
- resObj.type = 'getuserinfo'
- that.props.onGetUserInfo(resObj)
- }
- },
- })
- }
- },
- stopEvent() {},
- btnOnTap(e) {
- const that = this
- const tapEvent = processDataSet(e, this.props)
- if (this.props.openType === 'openSetting') {
- my.openSetting({
- success(res) {
- if (typeof that.props.onOpenSetting === 'function') {
- that.props.onOpenSetting({
- ...tapEvent,
- type: 'opensetting',
- detail: {
- authSetting: utils.mapAuthSetting(res.authSetting),
- },
- })
- }
- },
- })
- }
- this.props.catchTap && this.props.catchTap(tapEvent)
- this.props.onTap && this.props.onTap(tapEvent)
- },
- getPhone(e) {
- const eve = { ...e }
- my.getPhoneNumber({
- success: (res) => {
- if (typeof res.response === 'string') {
- const response = JSON.parse(res.response)
- if (response.response.code === '40001') {
- utils.warn('请去小程序开发管理后台的功能列表中添加获取电话功能', {
- apiName: 'button/bindgetphonenumber',
- errorType: 1,
- type: 'component',
- })
- }
- return false
- }
- eve.detail = res.response
- if (typeof this.props.onGetPhoneNumber === 'function') {
- this.props.onGetPhoneNumber(eve)
- }
- },
- fail(err) {
- throw err
- },
- })
- },
- getUserInfo(e) {
- const that = this
- // 获取用户信息
- const eve = { ...e }
- my.getAuthCode({
- scopes: 'auth_user',
- success: () => {
- my.getOpenUserInfo({
- success: (userInfo) => {
- eve.detail = {
- userInfo: {},
- rawData: '',
- }
- if (typeof userInfo.response === 'string') {
- const response = JSON.parse(userInfo.response)
- if (response.response.code === '40006') {
- utils.warn(
- '请去小程序开发管理后台的功能列表中添加会员信息功能',
- {
- apiName: 'button/bindgetuserinfo',
- errorType: 1,
- type: 'component',
- },
- )
- }
- return false
- }
- eve.detail.userInfo = { ...userInfo.response }
- eve.detail.userInfo.avatarUrl = eve.detail.userInfo.avatar
- delete eve.detail.userInfo.avatar
- eve.detail.rawData = JSON.stringify(eve.detail.userInfo)
- if (typeof that.props.onGetUserInfo === 'function') {
- that.props.onGetUserInfo(eve)
- }
- },
- fail(err) {
- throw err
- },
- })
- },
- })
- },
- },
- })
|