app-phone-binding.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <view class="app-phone-binding">
  3. <view class="app-bound dir-top-nowrap cross-center" v-if="bind">
  4. <image class="app-image" :src="wxapp_img.mall.binding"></image>
  5. <view class="app-phone-text dir-top-nowrap" >
  6. <text>已绑定手机</text>
  7. <text class="app-hone">{{phone}}</text>
  8. </view>
  9. <view class="app-button">
  10. <app-button type="important" round background="#ff4544" height="88" width="700" fontSize="32" @click="clearPhone(false)">更换绑定</app-button>
  11. </view>
  12. </view>
  13. <template v-else>
  14. <view class="app-unbound">
  15. <!-- #ifndef MP-BAIDU -->
  16. <view class="header dir-left-nowrap" v-if="is_manual_mobile_auth == 1">
  17. <view class="box-grow-1" @click="setActive(1)">
  18. <app-form-id>
  19. <text :class="{'active-index': activeIndex === 1}">
  20. 直接授权
  21. </text>
  22. </app-form-id>
  23. </view>
  24. <view class="box-grow-1" @click="setActive(2)">
  25. <app-form-id>
  26. <text :class="{'active-index': activeIndex === 2}">
  27. 手动输入
  28. </text>
  29. </app-form-id>
  30. </view>
  31. </view>
  32. <!-- #endif-->
  33. <view class="content">
  34. <!-- #ifndef MP-BAIDU -->
  35. <view class="input-view dir-left-nowrap cross-center" v-if="activeIndex === 1">
  36. <view class="input" :class="{'input-value': value !== ''}">{{value === '' ? '点击授权获取手机号' : value}}</view>
  37. <view class="line"></view>
  38. <!-- #ifndef MP-ALIPAY -->
  39. <button open-type="getPhoneNumber" class="button" @getphonenumber="getPhoneNumber">点击授权</button>
  40. <!-- #endif -->
  41. <!-- #ifdef MP-ALIPAY -->
  42. <button open-type="getAuthorize"
  43. class="button"
  44. @getAuthorize="onGetAuthorize"
  45. scope='phoneNumber'
  46. >点击授权</button>
  47. <!-- #endif -->
  48. </view>
  49. <!-- #endif -->
  50. <view class="manual-input dir-top-nowrap" v-if="activeIndex === 2">
  51. <view class="top">
  52. <input type="number" v-model="phoneNumber" placeholder="输入手机号">
  53. </view>
  54. <view class="bottom dir-left-nowrap cross-center">
  55. <input type="number" placeholder="输入验证码" v-model="verificationCode">
  56. <view class="line"></view>
  57. <view class="btn" @click="obtain">获取验证码</view>
  58. </view>
  59. </view>
  60. <view class="sure-button" @click="bindPhone(true, activeIndex)">确认</view>
  61. </view>
  62. </view>
  63. </template>
  64. </view>
  65. </template>
  66. <script>
  67. import {mapState, mapGetters} from 'vuex';
  68. export default {
  69. name: 'app-phone-binding',
  70. data() {
  71. return {
  72. code: '',
  73. value: '',
  74. // #ifndef MP-BAIDU
  75. activeIndex: 1,
  76. // #endif
  77. // #ifdef MP-BAIDU
  78. activeIndex: 2,
  79. // #endif
  80. phoneNumber: null,
  81. verificationCode: null,
  82. }
  83. },
  84. props: {
  85. bind: {
  86. type: Boolean,
  87. },
  88. phone: {
  89. type: String,
  90. }
  91. },
  92. computed: {
  93. ...mapState({
  94. wxapp_img: state => state.mallConfig.__wxapp_img,
  95. is_manual_mobile_auth: state => state.mallConfig.mall.setting.is_manual_mobile_auth
  96. }),
  97. },
  98. created() {
  99. // #ifndef MP-ALIPAY
  100. let _this= this;
  101. uni.login({
  102. scopes: 'auth_base',
  103. success(res) {
  104. if (res.errMsg === 'login:ok') {
  105. _this.code = res.code;
  106. }
  107. }
  108. })
  109. // #endif
  110. },
  111. methods: {
  112. // #ifndef MP-ALIPAY
  113. getPhoneNumber(e) {
  114. if (e.detail.errMsg === 'getPhoneNumber:fail user deny' || !this.code) return;
  115. let _this = this;
  116. this.$request({
  117. method: 'post',
  118. url: _this.$api.phone.binding,
  119. data: {
  120. encryptedData: e.detail.encryptedData,
  121. iv: e.detail.iv,
  122. code: _this.code,
  123. }
  124. }).then(response => {
  125. this.value = response.data.mobile;
  126. })
  127. },
  128. // #endif
  129. // #ifdef MP-ALIPAY
  130. onGetAuthorize() {
  131. let _this = this;
  132. my.getPhoneNumber({
  133. success: (res) => {
  134. this.$request({
  135. method: 'post',
  136. url: _this.$api.phone.binding,
  137. data: {
  138. data: JSON.parse(res.response).response,
  139. }
  140. });
  141. this.value = JSON.parse(res.response).response;
  142. },
  143. fail: () => {
  144. }
  145. });
  146. },
  147. // #endif
  148. bindPhone(data, i) {
  149. if (i === 1) {
  150. if (this.value === '') return;
  151. this.$emit('click', data);
  152. } else {
  153. if (this.verificationCode === null) return;
  154. this.$request({
  155. url: this.$api.phone.binding,
  156. method: 'post',
  157. data: {
  158. mobile: this.phoneNumber,
  159. mobile_code: this.verificationCode,
  160. }
  161. }).then(res => {
  162. if (res.code === 0) {
  163. this.$emit('click', data);
  164. } else if (res.code === 1) {
  165. uni.showModal({
  166. title: '提示',
  167. content: res.msg,
  168. })
  169. }
  170. });
  171. }
  172. },
  173. clearPhone(data) {
  174. this.$emit('click', data);
  175. },
  176. obtain() {
  177. if (this.phoneNumber === null) return;
  178. this.$request({
  179. url: this.$api.phone.code,
  180. data: {
  181. mobile: this.phoneNumber,
  182. }
  183. }).then(res => {
  184. if (res.code === 0) {
  185. uni.showModal({
  186. title: '提示',
  187. content: res.msg,
  188. })
  189. } else if (res.code === 1) {
  190. uni.showModal({
  191. title: '提示',
  192. content: res.msg,
  193. })
  194. }
  195. })
  196. },
  197. setActive(data) {
  198. this.activeIndex = data;
  199. }
  200. },
  201. }
  202. </script>
  203. <style scoped lang="scss">
  204. @import './app-phone-binding.scss';
  205. </style>