app&H5 获取code微信登录

App

<view @click="onLogin"></view>
<script>
	let weixinAuthService;
	export default {
		components: {},
		data() {
			return {
				hasWeixinAuth: false,
			};
		},
		onLoad() {
			// #ifdef APP
			plus.oauth.getServices(services => {
				weixinAuthService = services.find(service => {
					console.log('service===', service);
					return service.id === 'weixin';
				});
				if (weixinAuthService) {
					this.hasWeixinAuth = true;
				}
			});
			// #endif
		},
		methods: {
			// 微信登录
			async onLogin() {
				if (!this.hasWeixinAuth) return;
				this.getWeixinCode().then(code => {
					console.log('codelogin====', code);
					this.wxLoginApi(code);
				});
			},
			// 获取微信code
			getWeixinCode() {
				return new Promise((resolve, reject) => {
					// #ifdef MP-WEIXIN
					uni.login({
						provider: 'weixin',
						success(res) {
							resolve(res.code);
						},
						fail(err) {
							reject(new Error('微信登录失败'));
						}
					});
					// #endif
					// #ifdef APP-PLUS
					weixinAuthService.authorize(
						function(res) {
							resolve(res.code);
						},
						function(err) {
							console.log(err);
							reject(new Error('微信登录失败'));
						}
					);
					// #endif
				});
			},

			// app微信登录接口
			async wxLoginApi(value) {
				var res = await this.$post('登录接口地址', {
					code: value,
					type:1
				});
				if (res.errcode != 0) {
					this.$toast(res.errmsg);
					return;
				}
				this.$toast('登录成功');
				this.$store.set('jwt', res.data.jwt);
				setTimeout(() => {
					this.$replaceAllTo('登陆之后跳转页面地址', {});
				}, 1000);
			},

		}
	};
</script>

H5

<view @click="onLogin"></view>
export default {
		data() {
			return {
				
			}
		},
		onLoad(options) {
			let result = this.getAllParams()
			if (result.id) { //如果跳转来的链接有参数存到缓存里
			  this.$store.set('match_id', parseInt(result.id));
			} 
			if (result.code) { //onLogin()回调之后会返回code参数
			  this.wxLoginApi(result.code);
			} 
		},
	
		methods: {
			getAllParams() { //获取url地址上的key,value的方法
				let href = window.location.href;
				let query = href.substring(href.indexOf("?") + 1);
				let vars = query.split("&");
				let obj = {};
				for (let i = 0; i < vars.length; i++) {
					let pair = vars[i].split("=");
					// 将参数名和参数值分别作为对象的属性名和属性值
					obj[pair[0]] = pair[1];
				}
				return obj
			},
			// 获取微信授权
			async onLogin() {
				let appid = 'appid' //微信公众号的appid
				let redirect_uri = '当前项目地址(需要配置)' //  授权登录成功回调的地址,一般为当前页// 授权操作是直接访问腾讯开放平台的一个授权地址,授权成功后会回调 需要在公众平台配置
				window.location = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+ appid+'&redirect_uri='+redirect_uri+'&response_type=code&scope=snsapi_userinfo&state=abcdefghigklmnopqrstuvwxyz#wechat_redirect' //引导用户授权
			},
			// 微信登录
			async wxLoginApi(value) {
				var res = await this.$post('微信登录的接口地址', {
					code: value,
					type: 2
				});
				if (res.errcode != 0) {
					this.$toast(res.errmsg);
					return;
				}
				this.$toast('登录成功');
				this.$store.set('jwt', res.data.jwt);
				setTimeout(() => {
					uni.redirectTo({
						url: '登陆之后跳转页面地址'
					})
				}, 1000);
			},
		}
	}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2018-09-03 php服务端微信支付整理SDK,封装,如果喜欢请给个好评!谢谢 说明: 配置在 WeChatConfig文件下 环境:php5.6,基于tp5开发 调用统一下单接口: include_once EXTEND_PATH . 'WeChatSDK/WeChatSDK.php'; $data = $this->getOrderInfo($pay_sn); if (!$data) { return $this->resultCode(-2019, '订单不存在或已支付'); } $WeixinPay = new \WeChatSDK(); if ($trade_type == 'JSAPI') { //目前未有此功能 $openid = ''; $product_id = ''; } if ($trade_type == 'NATIVE') { $openid = ''; $product_id = $pay_sn; } if ($trade_type == 'MWEB') { $openid = ''; $product_id = $pay_sn; } if ($trade_type == 'APP') { $openid = ''; $product_id = $pay_sn; } $out_trade_no = $pay_sn; $result = $WeixinPay->setWeiXinPay($data['pay_body'], $data['pay_detail'], $data['pay_money'] * 100, $out_trade_no, $red_url, $trade_type, $openid, $product_id); APP加密:$WeChatSDK->GetAppParameters($result['data']); web编码 $WeChatSDK->GetMwebApiParameters(); jsapi:WeChatSDK-> GetJsApiParameters(); 回调调用: include_once EXTEND_PATH . 'WeChatSDK/WeChatSDK.php'; Log::write("gwgwgwgw---------------------------------进入异步回掉"); $postStr = file_get_contents('php://input'); Log::write("gwgwgwgw---------------------------------" . $postStr); $WeChatSDK = new \WeChatSDK(); if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $check_sign = $WeChatSDK->checkSign($postObj, $postObj->sign); Log::write('-----check_sign-------' . $check_sign . '------------check_sign--------------'); if ($postObj->result_code == 'SUCCESS' && $check_sign == 1) { model('order', 'service')->affirmPayment($postObj->out_trade_no); $xml = "<xml> <![CDATA[SUCCESS]]></return_co

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值