becomeVIP.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <view class="content hb column justify-center align-center bgf">
  3. <image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/image/becomevip.png" mode="widthFix"></image>
  4. <view class="bold">成为会员,享受更多权益</view>
  5. <!--#ifdef H5-->
  6. <view class="sure" @click="registerCourse">{{isVip==1?'您已成为会员':'申请成为会员'}}</view>
  7. <!--#endif-->
  8. <!--#ifdef MP-WEIXIN-->
  9. <view class="btns">
  10. <button
  11. class="author-btn"
  12. open-type="getPhoneNumber"
  13. @getphonenumber="phoneLogin" >{{isVip==1?'您已成为会员':'申请成为会员'}}</button>
  14. <button class="author-btn" v-if="isVip==1" @click="handleAgree()">{{isVip==1?'您已成为会员':'申请成为会员'}}</button>
  15. </view>
  16. <!--#endif-->
  17. </view>
  18. </template>
  19. <script>
  20. import { loginByMp,registerCourses,loginByMiniApp} from '@/api/user'
  21. export default {
  22. data() {
  23. return {
  24. isVip: 0,
  25. isWechat: false,
  26. isLogin: false,
  27. companyid:'',
  28. companyUserId:'',
  29. userInfo:{},
  30. tagIds:[]
  31. }
  32. },
  33. onLoad(option) {
  34. if(uni.getStorageSync('userInfo')&&JSON.stringify(uni.getStorageSync('userInfo'))!='{}') {
  35. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  36. } else {
  37. this.userInfo = {}
  38. }
  39. // this.userInfo=JSON.parse(uni.getStorageSync('userInfo')) || {};
  40. let obj=uni.getStorageSync('AppToken');
  41. this.isLogin = !!obj;
  42. this.companyid = option.companyId || ''
  43. this.companyUserId=option.companyUserId || 0
  44. if(option.tagids=="null"){
  45. this.tagIds=[]
  46. }else{
  47. this.tagIds=option.tagids
  48. const arr =this.tagIds.split(",")
  49. .map(item => parseInt(item.trim(), 10))
  50. .filter(num => !isNaN(num)); // 过滤无效转换
  51. this.tagIds =arr
  52. console.log(this.tagIds); // [66, 78]
  53. }
  54. this.isWechat = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"
  55. this.code = option.code
  56. // #ifdef H5
  57. if(this.code&&!this.isLogin) {
  58. this.loginByMp()
  59. }
  60. // #endif
  61. },
  62. onShow() {
  63. if(uni.getStorageSync('userInfo')&&JSON.stringify(uni.getStorageSync('userInfo'))!='{}') {
  64. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  65. } else {
  66. this.userInfo = {}
  67. }
  68. let obj=uni.getStorageSync('AppToken');
  69. this.isLogin = !!obj;
  70. if(this.isLogin&&this.isVip!=1) {
  71. this.registerCourse()
  72. }
  73. },
  74. methods: {
  75. handleAgree(){
  76. uni.showToast({
  77. title: '您已成为会员!',
  78. icon: 'none',
  79. duration: 2000,
  80. })
  81. },
  82. // 微信用户手机号登录
  83. phoneLogin(e) {
  84. var that=this;
  85. uni.showLoading({
  86. title:"处理中..."
  87. })
  88. if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
  89. this.utils.getProvider()
  90. .then(provider => {
  91. if (!provider) {
  92. reject()
  93. }
  94. // uni登录
  95. uni.login({
  96. provider: provider,
  97. success: async loginRes => {
  98. console.log(loginRes)
  99. let code = loginRes.code // 获取开发code
  100. loginByMiniApp({
  101. encryptedData: e.mp.detail.encryptedData,
  102. iv: e.mp.detail.iv,
  103. code: code,
  104. })
  105. .then( res => {
  106. if(res.code==200){
  107. console.log(res)
  108. uni.hideLoading();
  109. uni.showToast({
  110. icon:'none',
  111. title: "登录成功",
  112. });
  113. uni.setStorageSync('AppToken', res.token);
  114. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  115. this.userInfo=res.user;
  116. uni.hideLoading()
  117. this.isLogin = true
  118. setTimeout(()=>{
  119. this.registerCourse()
  120. },200)
  121. }
  122. else{
  123. uni.hideLoading();
  124. uni.showToast({
  125. icon:'none',
  126. title: "授权登录失败,请重新登录",
  127. });
  128. }
  129. })
  130. }
  131. })
  132. })
  133. .catch(err => {
  134. uni.showToast({
  135. icon:'none',
  136. title: err,
  137. });
  138. })
  139. } else {
  140. uni.showToast({
  141. title: '已拒绝授权',
  142. icon: 'none',
  143. duration: 2000,
  144. })
  145. }
  146. },
  147. getWechatCode() {
  148. if (this.isWechat) {
  149. let appid = "wx961fadab9bcb792b"; //微信APPid
  150. let code = this.getUrlCode().code; //是否存在code
  151. let local = window.location.href;
  152. if (code == null || code === "") {
  153. let urlPaths = local.split("/registerCourse");
  154. uni.setStorageSync('beforLoginPage', urlPaths[1]);
  155. //不存在就打开上面的地址进行授权
  156. window.location.href =
  157. "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
  158. appid +
  159. "&redirect_uri=" +
  160. encodeURIComponent(local) +
  161. "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  162. } else {
  163. this.code = code;
  164. this.loginByMp()
  165. }
  166. }else{
  167. uni.showToast({
  168. title: '请在微信浏览器中打开',
  169. icon:'error'
  170. })
  171. }
  172. },
  173. getUrlCode() {
  174. // 截取url中的code方法
  175. var url = location.search;
  176. var theRequest = new Object();
  177. if (url.indexOf("?") != -1) {
  178. var str = url.substr(1);
  179. var strs = str.split("&");
  180. for (var i = 0; i < strs.length; i++) {
  181. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  182. }
  183. }
  184. return theRequest;
  185. },
  186. loginByMp() {
  187. if (this.code == null) {
  188. return;
  189. }
  190. uni.showLoading({
  191. title: "处理中..."
  192. });
  193. loginByMp({code:this.code}).then(res => {
  194. uni.hideLoading();
  195. if (res.code == 200) {
  196. uni.setStorageSync('AppToken', res.token);
  197. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  198. this.userInfo= res.user
  199. let beforLoginUrl = uni.getStorageSync('beforLoginPage');
  200. this.isLogin = true
  201. this.registerCourse()
  202. } else {
  203. uni.showToast({
  204. title: res.msg,
  205. icon: 'none'
  206. });
  207. }
  208. },
  209. err => {}
  210. );
  211. },
  212. // 成为会员
  213. registerCourse() {
  214. this.isVip = 0
  215. // 确保从本地存储重新获取最新数据
  216. // if(uni.getStorageSync('userInfo')){
  217. // this.userInfo=JSON.parse(uni.getStorageSync('userInfo'));
  218. // }
  219. // if (!this.userInfo.userId) {
  220. // // 空值检查
  221. // uni.showToast({ title: '用户未登录或信息不完整', icon: 'none' });
  222. // return;
  223. // }
  224. if(this.tagIds==null){
  225. this.tagIds=""
  226. }
  227. console.log(this.tagIds)
  228. const data={
  229. userId:this.userInfo.userId,
  230. companyUserId:this.companyUserId,
  231. companyId:this.companyid,
  232. tagIds:this.tagIds
  233. }
  234. if(this.isLogin) {
  235. registerCourses(data).then(res=>{
  236. if(res.code == 200) {
  237. this.isVip = 1
  238. uni.showToast({
  239. title: '恭喜你!成为会员,请联系管理员!',
  240. icon:'none'
  241. })
  242. } else {
  243. uni.showToast({
  244. icon:'none',
  245. title: res.msg
  246. })
  247. }
  248. })
  249. } else {
  250. // #ifdef H5
  251. this.getWechatCode()
  252. // #endif
  253. }
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang="scss" scoped>
  259. .content {
  260. image {
  261. width: 300rpx;
  262. height: 300rpx;
  263. }
  264. }
  265. .bold {
  266. color: #999;
  267. font-size: 16px;
  268. height: auto;
  269. line-height: inherit;
  270. margin-bottom: 0;
  271. width: 304px;
  272. word-break: break-all;
  273. text-align: center;
  274. margin: 50rpx 0 100rpx 0;
  275. }
  276. .sure {
  277. width: 500rpx;
  278. background-color: #1777ff;
  279. line-height: 88rpx;
  280. text-align: center;
  281. border-radius: 8rpx;
  282. color: #fff;
  283. }
  284. .btns{
  285. position: relative;
  286. width: 630rpx;
  287. height: 80rpx;
  288. .author-btn{
  289. z-index:100;
  290. position: absolute;
  291. width: 630rpx;
  292. height: 80rpx;
  293. background: #1777ff;
  294. border-radius: 40rpx;
  295. font-size: 30rpx;
  296. font-family: PingFang SC;
  297. font-weight: 500;
  298. color: rgba(255, 255, 255, 1);
  299. }
  300. }
  301. </style>