Sys.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use library\traits\JwtAuthModelTrait;
  6. use library\traits\ModelTrait;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class Sys extends Model
  12. {
  13. use ModelTrait;
  14. use JwtAuthModelTrait;
  15. /**
  16. * 保存数据
  17. * @param $save
  18. */
  19. public function saveSys($save) {
  20. $this->where('id',1)->save($save);
  21. }
  22. /**
  23. * 微信配置信息
  24. * @param type $data
  25. */
  26. public function saveWxConfig($data){
  27. $save=[
  28. "appid" => empty($data["appid"])?"":$data["appid"],
  29. "secret" => empty($data["secret"])?"":$data["secret"],
  30. "mchid" => empty($data["mchid"])?"":$data["mchid"],
  31. "apiv2key" => empty($data["apiv2key"])?"":$data["apiv2key"],
  32. "apiclient_key" => empty($data["apiclient_key"])?"":$data["apiclient_key"],
  33. "apiclient_cert" => empty($data["apiclient_cert"])?"":$data["apiclient_cert"],
  34. "notify_url" => empty($data["notify_url"])?"":$data["notify_url"],
  35. ];
  36. $this->where("id",1)->update(["wxconfig"=> serialize($save)]);
  37. }
  38. /**
  39. * 分享配置信息
  40. * @param type $data
  41. */
  42. public function saveShareConfig($data){
  43. $save=[
  44. "img" => empty($data["img"]) ?"":$data["img"],
  45. "title" => empty($data["title"])?"":$data["title"],
  46. "query" => empty($data["query"])?"":$data["query"],
  47. "content" => empty($data["content"])?"":$data["content"],
  48. "is_show" => isset($data["is_show"]) ? $data["is_show"] : 0,
  49. ];
  50. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】传参: ".json_encode($data, JSON_UNESCAPED_UNICODE)." \r\n", 8);
  51. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】处理后: ".json_encode($save, JSON_UNESCAPED_UNICODE)." \r\n", 8);
  52. // 检查 id=1 的记录是否存在
  53. $exists = $this->where("id", 1)->find();
  54. if (!$exists) {
  55. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】id=1 不存在,执行插入 \r\n", 8);
  56. $result = $this->insert([
  57. "id" => 1,
  58. "shareconfig" => serialize($save)
  59. ]);
  60. } else {
  61. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】id=1 存在,执行更新 \r\n", 8);
  62. $result = $this->where("id", 1)->update(["shareconfig" => serialize($save)]);
  63. }
  64. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】更新结果: ".$result." \r\n", 8);
  65. return $result;
  66. }
  67. /**
  68. * 邀请配置
  69. * @param type $data
  70. */
  71. public function saveInviteConfig($data){
  72. $save=[
  73. "img" => empty($data["img"]) ? "" : $data["img"],
  74. ];
  75. $this->where("id",1)->update(["inviteconfig"=> serialize($save)]);
  76. }
  77. /**
  78. * 积分配置信息
  79. * @param type $data
  80. */
  81. public function saveScoreConfig($data){
  82. $points_transformation = empty($data["points_transformation"]) ?"":$data["points_transformation"];
  83. $points_share = empty($data["points_share"])?"":$data["points_share"];
  84. $this->where("id",1)->update(["points_transformation"=> $points_transformation,'points_share'=>$points_share]);
  85. }
  86. /**
  87. * 获取配置详情
  88. * @return type
  89. */
  90. public function getDataInfo($code=""){
  91. $sys = $this->where("id", 1)->find();
  92. //微信配置
  93. $wxconfig=[
  94. "appid"=>"",
  95. "secret"=>"",
  96. "mchid"=>"",
  97. "apiv2key"=>"",
  98. "apiclient_key"=>"",
  99. "apiclient_cert"=>"",
  100. "notify_url"=>""
  101. ];
  102. if(!empty($sys->wxconfig)){
  103. $wxconfig = unserialize($sys->wxconfig);
  104. }
  105. //分享配置
  106. $shareconfig=[
  107. "img"=>"",
  108. "title"=>"",
  109. "query"=>"",
  110. "content"=>"",
  111. "is_show"=>"",
  112. ];
  113. if(!empty($sys->shareconfig)){
  114. $shareconfig = unserialize($sys->shareconfig);
  115. }
  116. //邀请配置
  117. $inviteconfig=[
  118. "img"=>"",
  119. ];
  120. if(!empty($sys->inviteconfig)){
  121. $inviteconfig = unserialize($sys->inviteconfig);
  122. }
  123. if($code=="weixin"){
  124. return $wxconfig;
  125. }
  126. if($code=="share"){
  127. return $shareconfig;
  128. }
  129. if($code=="invite"){
  130. return $inviteconfig;
  131. }
  132. //积分配置
  133. $scoreconfig=[
  134. "points_transformation"=>"",
  135. ];
  136. if(!empty($sys->points_transformation)){
  137. $scoreconfig['points_transformation'] = $sys->points_transformation;
  138. }
  139. if($code=="score"){
  140. return $scoreconfig;
  141. }
  142. $data = $sys->toArray();
  143. $data["wxconfig"] = $wxconfig;
  144. $data["shareconfig"] = $shareconfig;
  145. $data["inviteconfig"] = $inviteconfig;
  146. return $data;
  147. }
  148. /**
  149. * 获取微信配置
  150. * @return bool
  151. */
  152. public function getWeixinConfig(){
  153. $data = $this->getDataInfo("weixin");
  154. foreach($data as $k=>$v){
  155. if(empty($v)){
  156. return false;
  157. }
  158. }
  159. $return=[
  160. "APPID" => $data["appid"],
  161. "APPSECRET" => $data["secret"],
  162. "MCHID" => $data["mchid"],
  163. "ApiV2Key" => $data["apiv2key"],
  164. "ApiclientKey" => "file://".app()->getRootPath()."/".trim($data["apiclient_key"],"/"),
  165. "ApiclientCert" => "file://".app()->getRootPath()."/".trim($data["apiclient_cert"],"/"),
  166. "NOTIFY_URL" => $data["notify_url"],
  167. ];
  168. return $return;
  169. }
  170. }