Util.php 5.1 KB

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