mini_id; if (!empty($mini_id)) { $store_mini = StoreAliMini::findOne($mini_id); if (empty($store_mini)) { return [ 'code' => 1, 'msg' => "查找不到小程序信息" ]; } $this->store_mini = $store_mini; $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one(); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取敏感字段使用场景 alipay.open.app.api.scene.query(查询接口字段使用场景) public function getField() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } $param = json_encode([ 'field_name' => 'mobile', 'api_name' => 'getPhoneNumber' ]); $request = new AlipayOpenAppApiSceneQueryRequest(); $result = $this->miniCommon($request, $param); if ($result->code == 10000) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result->auth_field_scene ]; } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //申请接入敏感字段 alipay.open.app.api.field.apply(申请获取接口用户敏感信息字段) public function applyPhone() { $t = \Yii::$app->db->beginTransaction(); try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } $store_mini = $this->store_mini; $param_info = $this->param_info; $request = new AlipayOpenAppApiFieldApplyRequest(); !empty($param_info['video']) && $request->setVideo('@' . $this->saveTempVideo($param_info['video'])); foreach ($param_info['picture'] as $index => $item) { switch ($index) { case 0: $request->setPicture1('@' . $this->saveTempImage($item)); break; case 1: $request->setPicture2('@' . $this->saveTempImage($item)); break; case 2: $request->setPicture3('@' . $this->saveTempImage($item)); break; case 3: $request->setPicture4('@' . $this->saveTempImage($item)); break; case 4: $request->setPicture5('@' . $this->saveTempImage($item)); break; } } $param = json_encode([ 'api_name' => "getPhoneNumber", 'field_name' => "mobile", "package_code" => "20180927110154092444", 'scene_code' => $param_info['scene_code'], 'qps_answer' => $param_info['qps_answer'], 'customer_answer' => $param_info['customer_answer'], 'memo' => $param_info['memo'], 'tiny_app_template_id' => Option::get("ali_template_appid", 0, 'saas')['value'] ]); $request->setAuthFieldApply($param); $result = $this->miniCommon($request); \Yii::error($result); $store_mini->apply_phone_msg = json_encode($param_info); $store_mini->is_get_phone = 1; if (!$store_mini->save()) { throw new \Exception(json_encode($store_mini->errors)); } if ($result->code == 10000) { $t->commit(); return [ 'code' => 0, 'msg' => "开始审核", 'data' => $result ]; } throw new \Exception($result->sub_msg); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //手动查询手机号申请状态 alipay.open.app.api.field.query(用户信息申请记录查询) public function getPhoneStatus() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //小程序model $store_mini = $this->store_mini; //请求接口 $request = new AlipayOpenAppApiFieldQueryRequest(); $result = $this->miniCommon($request); //请求成功 if ($result->code == 10000) { //获取申请状态 $data = $result->auth_field_response->records; $data_arr = json_decode(json_encode($data), true)[0]; $status = $data_arr['status']; //判断申请状态成功 if ($status === "AGREE") { //设置手机号申请状态 $store_mini->is_get_phone = 2; //AGREE通过,REJECT驳回,INVALID无效(isv代申请场景) } elseif ($status === "REJECT" || $status === "INVALID") { $reason = !empty($data_arr['reason']) ? $data_arr['reason'] : ''; $store_mini->is_get_phone = 3; $store_mini->apply_phone_fail = $reason; } if (!$store_mini->save()) { throw new \Exception($store_mini->errors); } return [ 'code' => 0, 'msg' => "保存成功", 'data' => $data, 'status' => $status ]; } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }