Util.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. class Util
  4. {
  5. # 生成订单号
  6. public function createNumber($prefix, $table, $where = [], $key = 'order_num')
  7. {
  8. $number = \Dever\Helper\Str::order($prefix);
  9. $where[$key] = $number;
  10. $state = Dever::db($table)->find($where);
  11. if (!$state) {
  12. return $number;
  13. }
  14. return $this->createNumber($prefix, $table, $where, $key);
  15. }
  16. # 获取某个setting值
  17. public function setting($key, $account, $project = 'api')
  18. {
  19. $setting = Dever::db('api/platform_setting')->find(['key' => $key]);
  20. $setting = Dever::db($project . '/account_setting')->find(['account_id' => $account, 'platform_setting_id' => $setting['id']]);
  21. $value = $setting['value'] ?? 0;
  22. return $value;
  23. }
  24. # 获取openid 仅jspai和小程序需要openid
  25. public function openid($account, $env, $uid, $result = [], $project = 'api')
  26. {
  27. $account = Dever::load(Account::class)->get($account, $project);
  28. if ($account) {
  29. $info = Dever::db('api/openid')->find(['account_id' => $account['id'], 'uid' => $uid, 'env' => $env]);
  30. if ($info) {
  31. $result['openid'] = $info['openid'];
  32. } else {
  33. # 这里需要修改
  34. if ($env == 3 || $env == 2) {
  35. # jsapi 一般需要oauth授权
  36. if (empty($result['t'])) {
  37. $result['t'] = \Dever\Helper\Secure::login($uid);
  38. }
  39. $result['account'] = $account['key'];
  40. $result['link'] = Dever::url('api/oauth.code', $result);
  41. } elseif ($env == 5) {
  42. # 小程序
  43. $param['js_code'] = Dever::input('applet_code', 'is_string', '登录信息');
  44. $data = Dever::load(Account::class)->run($account, 'applet_login', $param, $env, 'run', $project);
  45. if (isset($data['openid'])) {
  46. $result['openid'] = $data['openid'];
  47. $update['uid'] = $uid;
  48. $update['account_id'] = $account['id'];
  49. $update['env'] = $env;
  50. $update['openid'] = $data['openid'];
  51. Dever::db('api/openid')->insert($update);
  52. }
  53. }
  54. }
  55. }
  56. return $result;
  57. }
  58. # 获取token
  59. public function token($account, $env, $project = 'api')
  60. {
  61. $account = Dever::load(Account::clas)->get($account, $project);
  62. $result = '';
  63. if ($env == 5) {
  64. $appid = 'applet_appid';
  65. $secret = 'applet_secret';
  66. }
  67. $appid = $this->setting($appid, $account['id'], $project);
  68. $secret = $this->setting($secret, $account['id'], $project);
  69. if ($account && $appid && $secret) {
  70. $param = ['appid' => $appid, 'appsecret' => $secret];
  71. $info = Dever::db('api/token')->find($param);
  72. if ($info && time() <= $info['edate']) {
  73. $result = $info['token'];
  74. } else {
  75. $data = Dever::load(Account::clas)->run($account, 'access_token', $param, 1, 'run', $project);
  76. if (isset($data['access_token'])) {
  77. $result = $data['access_token'];
  78. $param['token'] = $result;
  79. $param['edate'] = time() + $data['expires_in'];
  80. if ($info) {
  81. Dever::db('api/token')->update($info['id'], $param);
  82. } else {
  83. Dever::db('api/token')->insert($param);
  84. }
  85. }
  86. }
  87. }
  88. return $result;
  89. }
  90. # 获取参数类型
  91. public function fieldType($platform_id)
  92. {
  93. $data = [];
  94. $data[] = array
  95. (
  96. 'id' => 1,
  97. 'name' => '格式转换',
  98. 'children' => Dever::db('api/format')->select([]),
  99. );
  100. $where = ['platform_id' => $platform_id];
  101. $data[] = array
  102. (
  103. 'id' => 2,
  104. 'name' => '加密',
  105. 'children' => Dever::db('api/platform_ssl')->select($where),
  106. );
  107. $data[] = array
  108. (
  109. 'id' => 3,
  110. 'name' => '签名',
  111. 'children' => Dever::db('api/platform_sign')->select($where),
  112. );
  113. return $data;
  114. }
  115. # 获取签名列表
  116. public function getPlatformSign($platform_id)
  117. {
  118. return Dever::db('api/platform_sign')->select(['platform_id' => $platform_id]);
  119. }
  120. # 格式转换
  121. public function format($id, $value)
  122. {
  123. $info = Dever::db('api/format')->find($id);
  124. if ($info) {
  125. $info['method'] = str_replace('{value}', "'{value}'", $info['method']);
  126. $value = \Dever\Helper\Str::val($info['method'], ['value' => $value]);
  127. }
  128. return $value;
  129. }
  130. }