index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view class="main">
  3. <view class="wx">
  4. <view class="wx_1">
  5. <text class="iconfont icon-weixin"></text>
  6. </view>
  7. <view class="wx_2">
  8. <button size="mini" @tap="otherLogin()">微信信任登录</button>
  9. </view>
  10. <view class="wx_3">
  11. <checkbox-group @change="changeAgree">
  12. <label>
  13. <checkbox :checked="agree" />
  14. <text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  15. </label>
  16. </checkbox-group>
  17. </view>
  18. </view>
  19. <!-- <uni-popup ref="dialogShow" type="center" :mask-click="false" background-color="#ffffff">
  20. <view class="popup">
  21. <view class="title">
  22. <text>绑定手机号</text>
  23. </view>
  24. <view class="wx_1">
  25. <text>确定获取微信绑定手机号吗?</text>
  26. </view>
  27. <view class="wx_2">
  28. <button size="mini" @tap="diaClose">取消</button>
  29. <button size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">确定</button>
  30. </view>
  31. </view>
  32. </uni-popup> -->
  33. </view>
  34. </template>
  35. <script>
  36. import moment from 'moment';
  37. export default {
  38. data() {
  39. return {
  40. openid: '',
  41. form: {},
  42. // 用户协议
  43. agree: false,
  44. // 注册账号信息
  45. user: {},
  46. // 弹框
  47. dialog: {
  48. title: '绑定手机号'
  49. },
  50. };
  51. },
  52. onShow: function() {
  53. const that = this;
  54. // 查询平台信息
  55. that.searchOpenids();
  56. },
  57. methods: {
  58. async searchOpenids() {
  59. const that = this;
  60. try {
  61. const res = uni.getStorageSync('openid');
  62. if (res) that.$set(that, `openid`, res);
  63. } catch (e) {
  64. uni.showToast({
  65. title: err.errmsg,
  66. icon: 'error',
  67. duration: 2000
  68. });
  69. }
  70. },
  71. // 其他登录方式
  72. async otherLogin() {
  73. const that = this;
  74. let agree = that.agree;
  75. let openid = that.openid;
  76. if (agree) {
  77. if (openid) {
  78. const res = await that.$api(`/user/login`, 'GET', {
  79. openid: openid
  80. })
  81. if (res.errcode == '0') {
  82. const token = await that.$token(`/tool/token`, 'GET', {
  83. token: res.data
  84. });
  85. if (token.errcode == '0') {
  86. let arr = await that.$api(`/user/${token.data._id}`, 'GET', {})
  87. if (!arr.data.nick_name) arr.data.nick_name = `微信用户${moment.valueOf()}`
  88. uni.setStorage({
  89. key: 'token',
  90. data: arr.data,
  91. success: function(res) {
  92. uni.navigateBack({
  93. delta: 1
  94. })
  95. },
  96. fail: function(err) {
  97. console.log(err);
  98. }
  99. })
  100. }
  101. } else {
  102. uni.showToast({
  103. title: res.errmsg || '信息错误',
  104. icon: 'none'
  105. })
  106. }
  107. } else {
  108. uni.showToast({
  109. title: '系统更新中,请稍后再试!',
  110. icon: 'none'
  111. })
  112. }
  113. } else {
  114. uni.showToast({
  115. title: '请阅读并同意用户协议和隐私政策',
  116. icon: 'none'
  117. })
  118. }
  119. },
  120. // 获取手机号
  121. getUserPhone(e) {
  122. console.log(e);
  123. const that = this;
  124. uni.login({
  125. provider: 'weixin',
  126. success: async function(data) {
  127. let res = await that.$api(`/wechat/api/login/getPhone`, 'GET', {
  128. config: that.$config.wx_projectkey,
  129. code: data.code
  130. })
  131. if (res.errcode == '0') {
  132. // 登录成功
  133. uni.getUserInfo({
  134. provider: 'weixin',
  135. success: function(info) {
  136. const phone = res.data && res.data.phone_info && res.data
  137. .phone_info
  138. .purePhoneNumber || '';
  139. // 修改用户信息
  140. that.updatePhone({
  141. phone,
  142. nick_name: info.userInfo.nickName,
  143. logo: [{
  144. url: info.userInfo.avatarUrl
  145. }],
  146. })
  147. }
  148. })
  149. } else {
  150. uni.showToast({
  151. title: res.errmsg,
  152. icon: 'none'
  153. })
  154. }
  155. },
  156. fail: function(err) {
  157. // 登录授权失败
  158. // err.code是错误码
  159. }
  160. });
  161. },
  162. async updatePhone(form) {
  163. const that = this;
  164. let user = that.user;
  165. let openid = that.openid;
  166. let arr = await that.$api(`/user/${user._id}`, 'POST', {
  167. openid,
  168. ...form
  169. })
  170. if (arr.errcode == '0') {
  171. let res = await that.$api(`/user/${user._id}`, 'GET', {})
  172. uni.setStorage({
  173. key: 'token',
  174. data: res.data,
  175. success: function(res) {
  176. uni.navigateBack({
  177. delta: 1
  178. })
  179. },
  180. fail: function(err) {
  181. console.log(err);
  182. }
  183. })
  184. } else {
  185. uni.showToast({
  186. title: res.errmsg,
  187. icon: 'none'
  188. })
  189. }
  190. },
  191. // 弹框关闭
  192. diaClose() {
  193. const that = this;
  194. that.$refs.dialogShow.close();
  195. },
  196. // 同意隐私协议
  197. changeAgree() {
  198. const that = this;
  199. let agree = !that.agree
  200. that.$set(that, `agree`, agree);
  201. },
  202. // 查看隐私协议
  203. toAgree() {
  204. const that = this;
  205. uni.navigateTo({
  206. url: `/pagesIndex/other/agree`
  207. })
  208. },
  209. },
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. .main {
  214. display: flex;
  215. flex-direction: column;
  216. width: 100vw;
  217. height: 100vh;
  218. .wx {
  219. text-align: center;
  220. margin: 25vw 0 0 0;
  221. .wx_1 {
  222. margin: 0 0 5vw 0;
  223. .iconfont {
  224. color: var(--f35BColor);
  225. }
  226. text {
  227. font-size: 50px;
  228. }
  229. }
  230. .wx_2 {
  231. button {
  232. background: var(--f35BColor);
  233. color: var(--fffColor);
  234. font-size: var(--font16Size);
  235. }
  236. }
  237. .wx_3 {
  238. position: absolute;
  239. bottom: 10vw;
  240. width: 100vw;
  241. text-align: center;
  242. font-size: 12px;
  243. }
  244. }
  245. .popup {
  246. width: 86vw;
  247. padding: 2vw;
  248. .title {
  249. text-align: center;
  250. margin: 0 0 2vw 0;
  251. text {
  252. color: var(--fFB1Color);
  253. font-size: var(--font16Size);
  254. }
  255. }
  256. .wx_1 {
  257. text-align: center;
  258. margin: 9vw 0;
  259. text {
  260. font-size: var(--font18Size);
  261. }
  262. }
  263. .wx_2 {
  264. text-align: center;
  265. button {
  266. margin: 0 2vw;
  267. font-size: var(--font16Size);
  268. padding: 0 10vw;
  269. color: var(--fffColor);
  270. background-color: var(--f35BColor);
  271. }
  272. button:nth-child(1) {
  273. background-color: var(--fFB1Color);
  274. }
  275. }
  276. }
  277. }
  278. </style>