PhoneMiniForm.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\third\ali;
  8. use app\models\Option;
  9. use app\models\StoreAliMini;
  10. use app\models\StoreAliMiniVersion;
  11. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiFieldApplyRequest;
  12. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiFieldQueryRequest;
  13. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiSceneQueryRequest;
  14. class PhoneMiniForm extends BaseForm
  15. {
  16. /**
  17. * 敏感字段form
  18. */
  19. public function inits()
  20. {
  21. try {
  22. $mini_id = $this->mini_id;
  23. if (!empty($mini_id)) {
  24. $store_mini = StoreAliMini::findOne($mini_id);
  25. if (empty($store_mini)) {
  26. return [
  27. 'code' => 1,
  28. 'msg' => "查找不到小程序信息"
  29. ];
  30. }
  31. $this->store_mini = $store_mini;
  32. $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
  33. }
  34. } catch (\Exception $e) {
  35. return [
  36. 'code' => 1,
  37. 'msg' => $e->getMessage()
  38. ];
  39. }
  40. }
  41. //获取敏感字段使用场景 alipay.open.app.api.scene.query(查询接口字段使用场景)
  42. public function getField()
  43. {
  44. try {
  45. $result = $this->inits();
  46. if ($result['code'] === 1) {
  47. return $result;
  48. }
  49. $param = json_encode([
  50. 'field_name' => 'mobile',
  51. 'api_name' => 'getPhoneNumber'
  52. ]);
  53. $request = new AlipayOpenAppApiSceneQueryRequest();
  54. $result = $this->miniCommon($request, $param);
  55. if ($result->code == 10000) {
  56. return [
  57. 'code' => 0,
  58. 'msg' => "成功",
  59. 'data' => $result->auth_field_scene
  60. ];
  61. }
  62. throw new \Exception($result->sub_msg);
  63. } catch (\Exception $e) {
  64. return [
  65. 'code' => 1,
  66. 'msg' => $e->getMessage()
  67. ];
  68. }
  69. }
  70. //申请接入敏感字段 alipay.open.app.api.field.apply(申请获取接口用户敏感信息字段)
  71. public function applyPhone()
  72. {
  73. $t = \Yii::$app->db->beginTransaction();
  74. try {
  75. $result = $this->inits();
  76. if ($result['code'] === 1) {
  77. return $result;
  78. }
  79. $store_mini = $this->store_mini;
  80. $param_info = $this->param_info;
  81. $request = new AlipayOpenAppApiFieldApplyRequest();
  82. !empty($param_info['video']) && $request->setVideo('@' . $this->saveTempVideo($param_info['video']));
  83. foreach ($param_info['picture'] as $index => $item) {
  84. switch ($index) {
  85. case 0:
  86. $request->setPicture1('@' . $this->saveTempImage($item));
  87. break;
  88. case 1:
  89. $request->setPicture2('@' . $this->saveTempImage($item));
  90. break;
  91. case 2:
  92. $request->setPicture3('@' . $this->saveTempImage($item));
  93. break;
  94. case 3:
  95. $request->setPicture4('@' . $this->saveTempImage($item));
  96. break;
  97. case 4:
  98. $request->setPicture5('@' . $this->saveTempImage($item));
  99. break;
  100. }
  101. }
  102. $param = json_encode([
  103. 'api_name' => "getPhoneNumber",
  104. 'field_name' => "mobile",
  105. "package_code" => "20180927110154092444",
  106. 'scene_code' => $param_info['scene_code'],
  107. 'qps_answer' => $param_info['qps_answer'],
  108. 'customer_answer' => $param_info['customer_answer'],
  109. 'memo' => $param_info['memo'],
  110. 'tiny_app_template_id' => Option::get("ali_template_appid", 0, 'saas')['value']
  111. ]);
  112. $request->setAuthFieldApply($param);
  113. $result = $this->miniCommon($request);
  114. \Yii::error($result);
  115. $store_mini->apply_phone_msg = json_encode($param_info);
  116. $store_mini->is_get_phone = 1;
  117. if (!$store_mini->save()) {
  118. throw new \Exception(json_encode($store_mini->errors));
  119. }
  120. if ($result->code == 10000) {
  121. $t->commit();
  122. return [
  123. 'code' => 0,
  124. 'msg' => "开始审核",
  125. 'data' => $result
  126. ];
  127. }
  128. throw new \Exception($result->sub_msg);
  129. } catch (\Exception $e) {
  130. $t->rollBack();
  131. return [
  132. 'code' => 1,
  133. 'msg' => $e->getMessage()
  134. ];
  135. }
  136. }
  137. //手动查询手机号申请状态 alipay.open.app.api.field.query(用户信息申请记录查询)
  138. public function getPhoneStatus()
  139. {
  140. try {
  141. $result = $this->inits();
  142. if ($result['code'] === 1) {
  143. return $result;
  144. }
  145. //小程序model
  146. $store_mini = $this->store_mini;
  147. //请求接口
  148. $request = new AlipayOpenAppApiFieldQueryRequest();
  149. $result = $this->miniCommon($request);
  150. //请求成功
  151. if ($result->code == 10000) {
  152. //获取申请状态
  153. $data = $result->auth_field_response->records;
  154. $data_arr = json_decode(json_encode($data), true)[0];
  155. $status = $data_arr['status'];
  156. //判断申请状态成功
  157. if ($status === "AGREE") {
  158. //设置手机号申请状态
  159. $store_mini->is_get_phone = 2;
  160. //AGREE通过,REJECT驳回,INVALID无效(isv代申请场景)
  161. } elseif ($status === "REJECT" || $status === "INVALID") {
  162. $reason = !empty($data_arr['reason']) ? $data_arr['reason'] : '';
  163. $store_mini->is_get_phone = 3;
  164. $store_mini->apply_phone_fail = $reason;
  165. }
  166. if (!$store_mini->save()) {
  167. throw new \Exception($store_mini->errors);
  168. }
  169. return [
  170. 'code' => 0,
  171. 'msg' => "保存成功",
  172. 'data' => $data,
  173. 'status' => $status
  174. ];
  175. }
  176. throw new \Exception($result->sub_msg);
  177. } catch (\Exception $e) {
  178. return [
  179. 'code' => 1,
  180. 'msg' => $e->getMessage()
  181. ];
  182. }
  183. }
  184. }