作者 杨育虎

小程序登录和支付

... ... @@ -13,11 +13,66 @@ use think\Validate;
class PublicController extends RestBaseController
{
// 微信小程序用户登录 TODO 增加最后登录信息记录,如 ip
/**
* @title 获取sessionKey
* @description 小程序登录注册
* @author Tiger Yang
* @url /wxapp/public/getSessionKey
* @method POST
*
* @param name:code type:string require:1 other: desc:code
*
* @return session_key:session_key
*/
public function getSessionKey(){
$validate = new Validate([
'code' => 'require',
]);
$validate->message([
'code.require' => '缺少参数code!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
$code = $data['code'];
$appId = config('app_id');
$appSecret = config('app_secret');
$response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
$response = json_decode($response, true);
if (!empty($response['errcode'])) {
$this->error(['code'=>'41001','msg'=>'操作失败:'.$response['errcode']]);
}
$this->success('获取成功',$response);
}
/**
* @title 小程序登录注册
* @description 小程序登录注册
* @author Tiger Yang
* @url /wxapp/public/login
* @method POST
*
* @param name:openid type:string require:1 other: desc:openid
* @param name:session_key type:string require:1 other: desc:session_key
* @param name:encrypted_data type:string require:1 other: desc:encrypted_data
* @param name:iv type:string require:1 other: desc:iv
* @param name:raw_data type:string require:1 other: desc:raw_data
* @param name:signature type:string require:1 other: desc:signature
*
* @return token:登录唯一标识
* @return user_type:用户类型[2-用户,3-员工,4-技师]
*/
public function login()
{
$validate = new Validate([
'code' => 'require',
'openid' => 'require',
'session_key' => 'require',
'encrypted_data' => 'require',
'iv' => 'require',
'raw_data' => 'require',
... ... @@ -25,7 +80,8 @@ class PublicController extends RestBaseController
]);
$validate->message([
'code.require' => '缺少参数code!',
'openid.require' => '缺少参数openid!',
'session_key.require' => '缺少参数session_key!',
'encrypted_data.require' => '缺少参数encrypted_data!',
'iv.require' => '缺少参数iv!',
'raw_data.require' => '缺少参数raw_data!',
... ... @@ -34,29 +90,19 @@ class PublicController extends RestBaseController
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
//TODO 真实逻辑实现
$code = $data['code'];
$appId = '你的 appid';
$appSecret = '你的 secket';
$response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
$response = json_decode($response, true);
if (!empty($response['errcode'])) {
$this->error('操作失败!');
}
$appId = config('app_id');
$openid = $response['openid'];
$sessionKey = $response['session_key'];
$openid = $data['openid'];
$sessionKey = $data['session_key'];
$pc = new WXBizDataCrypt($appId, $sessionKey);
$errCode = $pc->decryptData($data['encrypted_data'], $data['iv'], $wxUserData);
if ($errCode != 0) {
$this->error('操作失败!');
$this->error(['code'=>'41002','msg'=>'检验数据失败!'],['errCode'=>$errCode,'param'=>$data]);
}
$findThirdPartyUser = Db::name("third_party_user")
... ... @@ -71,7 +117,19 @@ class PublicController extends RestBaseController
unset($wxUserData['watermark']);
if ($findThirdPartyUser) {
$user_type=Db::name('user')->where(['id'=>$findThirdPartyUser['user_id']])->value('user_type');
switch ($user_type){
case 3:
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'staff');
break;
case 4:
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'technician');
break;
default :
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'wxapp');
break;
}
$userData = [
'last_login_ip' => $ip,
... ... @@ -88,10 +146,10 @@ class PublicController extends RestBaseController
->where('openid', $openid)
->where('app_id', $appId)
->update($userData);
$this->success("登录成功!", ['token' => $token,'user_type'=>$user_type]);
} else {
//TODO 使用事务做用户注册
Db::startTrans();
$userId = Db::name("user")->insertGetId([
'create_time' => $currentTime,
'user_status' => 1,
... ... @@ -100,10 +158,10 @@ class PublicController extends RestBaseController
'user_nickname' => $wxUserData['nickName'],
'avatar' => $wxUserData['avatarUrl'],
'last_login_ip' => $ip,
'last_login_time' => $currentTime,
'last_login_time' => $currentTime
]);
Db::name("third_party_user")->insert([
$row=Db::name("third_party_user")->insert([
'openid' => $openid,
'user_id' => $userId,
'third_party' => 'wxapp',
... ... @@ -117,12 +175,16 @@ class PublicController extends RestBaseController
'more' => json_encode($wxUserData)
]);
if($userId && $row){
Db::commit();
$token = cmf_generate_user_token($userId, 'wxapp');
$this->success("登录成功!", ['token' => $token,'user_type'=>2]);
}else{
Db::rollback();
$this->error(['code'=>'40004','msg'=>'登录失败']);
}
$this->success("登录成功!", ['token' => $token]);
}
}
... ...
... ... @@ -11,23 +11,24 @@ class WeixinPay {
protected $body;
protected $total_fee;
protected $notify_url;
function __construct($openid,$out_trade_no,$body,$total_fee,$notify_url) {
$this->appid = config('mini_app_id');
$this->mch_id = config('mch_id');
$this->key = config('key');
function __construct() {
$this->appid = config('app_id');
$this->mch_id = config('wx_mch_id');
$this->key = config('wx_pay_key');
}
public function pay($openid,$out_trade_no,$body,$total_fee,$notify_url) {
$this->openid = $openid;
$this->out_trade_no = $out_trade_no;
$this->body = $body;
$this->total_fee = $total_fee;
$this->notify_url = $notify_url;
}
public function pay() {
//统一下单接口
$return = $this->weixinapp();
return $return;
}
//统一下单接口
private function unifiedorder() {
protected function unifiedorder() {
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$parameters = array(
'appid' => $this->appid, //小程序ID
... ... @@ -91,7 +92,7 @@ class WeixinPay {
return $xml;
}
//xml转换成数组
private function xmlToArray($xml) {
public function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
... ... @@ -102,6 +103,9 @@ class WeixinPay {
private function weixinapp() {
//统一下单接口
$unifiedorder = $this->unifiedorder();
if(isset($unifiedorder['return_code'])&&$unifiedorder['return_code']=='FAIL'){
return $unifiedorder;
}
$parameters = array(
'appId' => $this->appid, //小程序ID
'timeStamp' => '' . time() . '', //时间戳
... ... @@ -123,7 +127,7 @@ class WeixinPay {
return $str;
}
//作用:生成签名
private function getSign($Obj) {
public function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
... ...