追格小程序绑定手机号提示缺少参数之微信获取手机号接口安全更新

煎饼/ 2023年05月28日/ 小程序/ 浏览 1085

有用户反馈追格小程序绑定手机号提示缺少参数,追格技术同学检查代码并无异常,可能还是配置问题。


查阅了一下微信小程序官网API,根据微信提示,从基础库 2.21.2 开始,对获取手机号的接口进行了安全升级,旧版本接口目前可以继续使用,但建议开发者使用新版本接口,以增强小程序安全性。


虽旧版本不影响使用,本着负责与安全考虑,追格技术同学对接口做了升级,若有需求可参考如下流程操作,本接口将在追格小程序V1.7.0后的版本同步更新。


1、前端:/pages/user/login/login.vue


			/**
			 * 获取手机号
			 */
			getPhoneNumber(e) {
				Rest.post(Api.URL('user', 'set_mobile'), {
					encrypted_data: e.detail.encryptedData,
					iv: e.detail.iv,
					code: this.code,
				}).then(res => {
					Alert.toast(res.message)

					// 更新本地缓存的信息
					let user = Auth.getUser();
					user.mobile = res.data.mobile;
					Auth.setUser(user);

					uni.$emit('zhuige_event_user_mobile', {
						mobile: res.data.mobile
					});
					Util.navigateBack();
				})
			}
		}
	}
</script>


【改成如下】

			/**
			 * 获取手机号
			 */
			getPhoneNumber(e) {
				if (e.detail.errMsg != 'getPhoneNumber:ok') {
					Alert.toast(e.detail.errMsg)
					return;
				}
				
				// Rest.post(Api.URL('user', 'set_mobile'), {
				// 	encrypted_data: e.detail.encryptedData,
				// 	iv: e.detail.iv,
				// 	code: this.code,
				// }).then(res => {
				// 	Alert.toast(res.message)

				// 	// 更新本地缓存的信息
				// 	let user = Auth.getUser();
				// 	user.mobile = res.data.mobile;
				// 	Auth.setUser(user);

				// 	uni.$emit('zhuige_event_user_mobile', {
				// 		mobile: res.data.mobile
				// 	});
				// 	Util.navigateBack();
				// })
				
				Rest.post(Api.URL('user', 'set_mobile2'), {
					code: e.detail.code
				}).then(res => {
					Alert.toast(res.message)
				
					// 更新本地缓存的信息
					let user = Auth.getUser();
					user.mobile = res.data.mobile;
					Auth.setUser(user);
				
					uni.$emit('zhuige_event_user_mobile', {
						mobile: res.data.mobile
					});
					Util.navigateBack();
				})
			}
		}
	}
</script>


2、后端/zhuige-xcx/public/rest/class-zhuige-xcx-user-controller.php


	/**
	 * 设置手机号2
	 */
	public function set_mobile2($request)
	{
		$user_id = get_current_user_id();

		$code = $this->param($request, 'code', '');
		if (empty($code)) {
			return $this->error('缺少参数');
		}

		$os = $this->param($request, 'os', '');
		$mobile = '';
		if ($os == 'wx') {
			$wx_session = ZhuiGe_Xcx::get_wx_token();
			if (!$wx_session) {
				return $this->success(['acode' => ZHUIGE_XCX_BASE_URL . 'public/images/placeholder.jpg']);
			}
		} else {
			return $this->error('暂不支持此平台');
		}


【改成如下】

	/**
	 * 设置手机号2
	 */
	public function set_mobile2($request)
	{
		$user_id = get_current_user_id();

		$code = $this->param($request, 'code', '');
		if (empty($code)) {
			return $this->error('缺少参数');
		}

		$os = $this->param($request, 'os', '');
		if ($os != 'wx') {
			return $this->error('暂不支持此平台');
		}

		$wx_session = ZhuiGe_Xcx::get_wx_token();
		if (!$wx_session) {
			return $this->error('获取不到token');
		}


发表评论

暂无评论,抢个沙发...

客服 工单